package com.dang.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.dang.dao.ProductDAO;
import com.dang.model.Book;
import com.dang.model.Product;
import com.dang.util.DBConnection;

public class ProductDAOImp implements ProductDAO {
	private final String FIND_BY_ID="select * from d_product p left join d_book b on b.id =p.id where p.id=?";
	private final String FIND_BY_HOT="select sum(product_num) as num,p.*,b.* from d_item i join d_product p on i.product_id=p.id join d_book b on i.product_id=b.id group by product_id order by num desc limit 0,?";
	private final String FIND_BY_NEW="select p.*,b.* from d_product p join d_book b on p.id=b.id order by p.add_time desc limit 0,?";
	private final String FIND_BY_NEW_HOT="select sum(product_num) as num,p.*,b.* from d_item i join d_product p on i.product_id=p.id join d_book b on i.product_id=b.id where p.add_time>? group by product_id order by num desc limit 0,?";
	@Override
	public Book findById(int id) throws Exception{
		PreparedStatement prep=null;
		Connection conn=null;
		Book book=null;
		ResultSet rst=null;
		try{
			conn=DBConnection.getConnection();
			prep=conn.prepareStatement(FIND_BY_ID);
			prep.setInt(1, id);
			rst=prep.executeQuery();
			if(rst.next()){
				book=new Book();
				book.setProductName(rst.getString("product_name"));
				book.setAuthor(rst.getString("author"));
				book.setAuthorSummary(rst.getString("author_summary"));
				book.setCatalogue(rst.getString("catalogue"));
				book.setId(rst.getInt("id"));
				book.setIsbn(rst.getString("isbn"));
				book.setPrintNumber(rst.getString("print_number"));
				book.setPrintDt(rst.getLong("print_time"));
				book.setPublishing(rst.getString("publishing"));
				book.setPublishDt(rst.getLong("publish_time"));
				book.setTotalPage(rst.getString("total_page"));
				book.setWhichEdtion(rst.getString("which_edtion"));
				book.setWordNumber(rst.getString("word_number"));
				book.setProductName(rst.getString("product_name"));
				book.setDescription(rst.getString("description"));
				book.setAddTime(rst.getLong("add_time"));
				book.setFixedPrice(rst.getDouble("fixed_price"));
				book.setDangPrice(rst.getDouble("dang_price"));
				book.setKeywords(rst.getString("keywords"));
				book.setHasDeleted(rst.getInt("has_deleted"));
				book.setProductPic(rst.getString("product_pic"));
			}
		}finally{
			DBConnection.close(prep, conn);
		}
		return book;
	}
	@Override
	public List<Product> findByHot(int hot) throws Exception {
		// TODO Auto-generated method stub
		PreparedStatement prep=null;
		Connection conn=null;
		Book book=null;
		ResultSet rst=null;
		List<Product> books=new ArrayList<>();
		try{
			conn=DBConnection.getConnection();
			prep=conn.prepareStatement(FIND_BY_HOT);
			prep.setInt(1,hot);
			rst=prep.executeQuery();
			while(rst.next()){
				book=new Book();
				book.setProductName(rst.getString("product_name"));
				book.setAuthor(rst.getString("author"));
				book.setAuthorSummary(rst.getString("author_summary"));
				book.setCatalogue(rst.getString("catalogue"));
				book.setId(rst.getInt("id"));
				book.setIsbn(rst.getString("isbn"));
				book.setPrintNumber(rst.getString("print_number"));
				book.setPrintDt(rst.getLong("print_time"));
				book.setPublishing(rst.getString("publishing"));
				book.setPublishDt(rst.getLong("publish_time"));
				book.setTotalPage(rst.getString("total_page"));
				book.setWhichEdtion(rst.getString("which_edtion"));
				book.setWordNumber(rst.getString("word_number"));
				book.setProductName(rst.getString("product_name"));
				book.setDescription(rst.getString("description"));
				book.setAddTime(rst.getLong("add_time"));
				book.setFixedPrice(rst.getDouble("fixed_price"));
				book.setDangPrice(rst.getDouble("dang_price"));
				book.setKeywords(rst.getString("keywords"));
				book.setHasDeleted(rst.getInt("has_deleted"));
				book.setProductPic(rst.getString("product_pic"));
				books.add(book);
			}
		} finally{
			DBConnection.close(prep, conn);
		}
		return books;
	}
	@Override
	public List<Product> findByNew(int news) throws Exception {
		// TODO Auto-generated method stub

		// TODO Auto-generated method stub
		PreparedStatement prep=null;
		Connection conn=null;
		Book book=null;
		ResultSet rst=null;
		List<Product> books=new ArrayList<>();
		try{
			conn=DBConnection.getConnection();
			prep=conn.prepareStatement(FIND_BY_NEW);
			prep.setInt(1,news);
			rst=prep.executeQuery();
			while(rst.next()){
				book=new Book();
				book.setProductName(rst.getString("product_name"));
				book.setAuthor(rst.getString("author"));
				book.setAuthorSummary(rst.getString("author_summary"));
				book.setCatalogue(rst.getString("catalogue"));
				book.setId(rst.getInt("id"));
				book.setIsbn(rst.getString("isbn"));
				book.setPrintNumber(rst.getString("print_number"));
				book.setPrintDt(rst.getLong("print_time"));
				book.setPublishing(rst.getString("publishing"));
				book.setPublishDt(rst.getLong("publish_time"));
				book.setTotalPage(rst.getString("total_page"));
				book.setWhichEdtion(rst.getString("which_edtion"));
				book.setWordNumber(rst.getString("word_number"));
				book.setProductName(rst.getString("product_name"));
				book.setDescription(rst.getString("description"));
				book.setAddTime(rst.getLong("add_time"));
				book.setFixedPrice(rst.getDouble("fixed_price"));
				book.setDangPrice(rst.getDouble("dang_price"));
				book.setKeywords(rst.getString("keywords"));
				book.setHasDeleted(rst.getInt("has_deleted"));
				book.setProductPic(rst.getString("product_pic"));
				books.add(book);
			}
		} finally{
			DBConnection.close(prep, conn);
		}
		return books;
	}


}
